<?php
/**
 *ContentPage Model
 *fetches data for content pages from database
 *
 *@author S.Feil, T.Ziegler, M.El-Hindi <goatymail@googlemail.com>
 *27 May 2012
 *@copyright Copyright (c) 2012, S.Feil, T.Ziegler, M.El-Hindi
 *@package cms
 *
 */
/**
 *Class ContentPage_Model
 *
 *@package model
 */
class ContentPage_Model extends Model{

    /**
     * Holds connection to database
     * @var object
     */
    private $oDBInstance;

    /**
     * Model constructor
     * Establishes connection to database
     */
    public function __construct(){
        $this->oDBInstance = Mysql_dbconnection::getInstance();
    }

    /**
     * Returns the title of the ContentPage with the given id
     * @param integer $iPage the page id of the page
     * @param integer $iLanguage the language of the title the that should be returned
     * @return string
     */
    public function getPageTitle($iPage, $iLanguage){
        //Sets table for query
        $this->oDBInstance->_setDefaultTable(
            	"(SELECT id, name, lid
            	  FROM bs_page INNER JOIN bs_page_language ON id = pid
            	  WHERE deleted = 0 AND visible = 1) as a");
        $this->oDBInstance->_setDefaultColumn('id');

        $sParentID = $this->oDBInstance->getSQLValueString($iPage,'int');
        $this->oDBInstance->performSelect('name', '', "id = $iPage AND lid = $iLanguage");
        $aResult = $this->oDBInstance->fetch('array');
        if(isset($aResult[0]['name'])){
            return $aResult[0]['name'];
        }else{
            return "Ung&uuml;ltiger Link.";
        }
    }
    /**
     * Fetch content of page from database
     * @param integer $iPage
     * @param integer $iLanguage
     * @return string
     */
    public function getPageContent($iPage, $iLanguage){
        //Sets default table
        $this->oDBInstance->_setDefaultTable(
                "(SELECT id, content, lid
                  FROM bs_page INNER JOIN bs_page_language ON id = pid
                  WHERE deleted = 0 AND visible = 1) as a");
        $this->oDBInstance->_setDefaultColumn('id');

        $sParentID = $this->oDBInstance->getSQLValueString($iPage,'int');
        //Performs query on database
        $this->oDBInstance->performSelect('content', '', "id = $iPage AND lid = $iLanguage");
        $aResult = $this->oDBInstance->fetch('array');
        //Fetches result of query
        if(isset($aResult[0]['content'])){
            return $aResult[0]['content'];
        }else{
            return "";
        }

    }
}
